<?php
namespace Admin\Model;
use Think\Model;
class MemberModel extends Model 
{
	protected $insertFields = array('username','password','cpassword','chkcode','must_click');
	protected $updateFields = array('id','username','password','cpassword');
	protected $_validate = array(
        array('must_click','require','必须同意注册协议',1,'regex',3),
		array('username', 'require', '用户名不能为空！', 1, 'regex', 3),
		array('username', '1,30', '用户名的值最长不能超过 30 个字符！', 1, 'length', 3),
		array('password', 'require', '密码不能为空！', 1, 'regex', 3),
		array('password', '6,20', '密码的值必须在6-20个字符之间！', 1, 'length', 3),
        array('cpassword','password','两次输入的密码不一致',1,'confirm',3),
        array('username', '', '用户名已经存在！', 1, 'unique', 3),
        array('chkcode', 'require', '验证码不能为空！', 1, ),
        array('chkcode', 'check_verify', '验证码不正确！', 1,'callback' ),
	);
    public $_login_validate = array(
        array('username', 'require', '用户名不能为空！', 1, 'regex', 3),
        array('password', 'require', '密码不能为空！', 1, 'regex', 3),
        array('chkcode', 'require', '验证码不能为空！', 1, ),
        array('chkcode', 'check_verify', '验证码不正确！', 1,'callback' ),
    );
    function check_verify($code,$id=''){
        $verify =new \Think\Verify();
        return $verify->check($code,$id);
    }

    public function login($needPasswords=true){
        $username = I('post.username');
        $password = I('post.password');
        $memberModel = D('Member');
        $user = $memberModel->field('id,username,password,jifen')
            ->where(array('username'=>array('eq',$username)))->find();
        if($user){
            if($needPasswords){
                if($user['password'] == md5($password)){
                    session('m_id',$user['id']);
                    session('m_username',$user['username']);
                    session('face','/Public/Home/images/user1.gif');
                    $mlModel = D('member_level');
                    $levelId = $mlModel->field('id')->where(
                        array(
                            'jifen_bottom'=>array('elt',$user['jifen']),
                            'jifen_top'=> array('egt',$user['jifen'])
                        )
                    )->find();
                    session('level_id',$levelId['id']);
                    $cartModel = D('Admin/Cart');
                    $cartModel->moveDataToDb();
                    return true;
                }else{
                    $this->error='密码不正确';
                    return false;
                }
            }else{
                session('m_id',$user['id']);
                session('m_username',$user['username']);
                session('face','/Public/Home/images/user1.gif');
                $mlModel = D('member_level');
                $levelId = $mlModel->field('id')->where(
                    array(
                        'jifen_bottom'=>array('elt',$user['jifen']),
                        'jifen_top'=> array('egt',$user['jifen'])
                    )
                )->find();
                session('level_id',$levelId['id']);
                $cartModel = D('Admin/Cart');
                $cartModel->moveDataToDb();
                return true;
            }
        }else{
            $this->error='用户名不存在';
            return false;
        }
    }
    public function logout(){
        session(null);
    }
    // 添加前
    protected function _before_insert(&$data, $option)
    {
        $data['password'] =md5($data['password']);
    }
    // 修改前
    protected function _before_update(&$data, $option)
    {
        if($data['password']){
            $data['password'] =md5($data['password']);
        }else{
            unset($data['password']);
        }
    }
	/************************************ 其他方法 ********************************************/
}